using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class ZooNotebookUI : MonoBehaviour
{
    public static ZooNotebookUI Instance;

    [Header("Notebook UI")]
    public GameObject notebookPanel;
    public Image leftImage;
    public TMP_Text leftLabel;
    public Image rightImage;
    public TMP_Text rightLabel;
    public StampShaderController leftStamp;
    public StampShaderController rightStamp;

    [Header("Page Navigation")]
    public Button arrowLeft;
    public Button arrowRight;

    [Header("Page Clicks")]
    [SerializeField] private Button leftButton;
    [SerializeField] private Button rightButton;

    [Header("Stamp Material")]
    [SerializeField] private Material notebookStampMaterial;


    private int currentPage = 0;


    void Awake()
    {
        if (Instance == null) Instance = this;
        else Destroy(gameObject);
    }

    // Called whenever animals change or notebook opens
    public void RefreshNotebook()
    {
        var list = ZooNotebookManager.Instance.collectedAnimals;
        int count = list.Count;

        // No animals? hide everything
        if (count == 0)
        {
            ClearPage();
            arrowLeft.gameObject.SetActive(false);
            arrowRight.gameObject.SetActive(false);
            leftButton.interactable = false;
            rightButton.interactable = false;
            return;
        }

        // Clamp page index
        int maxPage = Mathf.Max(0, (count - 1) / 2);
        currentPage = Mathf.Clamp(currentPage, 0, maxPage);

        int leftIndex = currentPage * 2;
        int rightIndex = leftIndex + 1;

        // -------------------------
        // LEFT PAGE
        // -------------------------
        if (leftIndex < count)
        {
            var entry = list[leftIndex];
            leftStamp.ApplyStamp(list[leftIndex].spriteAnimal, list[leftIndex].name);
            leftLabel.text = list[leftIndex].name;
            leftImage.gameObject.SetActive(true);
            leftLabel.gameObject.SetActive(true);

            // apply stamp material
            Material leftMat = new Material(notebookStampMaterial);
            leftMat.SetFloat("_Seed", entry.stampSeed);
            leftImage.material = leftMat;


            // enable click -> loads animal scene
            leftButton.interactable = true;
            leftButton.onClick.RemoveAllListeners();
            leftButton.onClick.AddListener(() =>
            {
                ZooUIController.instance.ShowAnimalFromNotebook(entry);
                notebookPanel.SetActive(false);
            });
        }
        else
        {
            leftImage.gameObject.SetActive(false);
            leftLabel.gameObject.SetActive(false);
            leftButton.interactable = false;
        }

        // -------------------------
        // RIGHT PAGE
        // -------------------------
        if (rightIndex < count)
        {
            var entry = list[rightIndex];
            rightStamp.ApplyStamp(list[rightIndex].spriteAnimal, list[rightIndex].name);
            rightLabel.text = list[rightIndex].name;
            rightImage.gameObject.SetActive(true);
            rightLabel.gameObject.SetActive(true);

            // apply stamp material
            Material rightMat = new Material(notebookStampMaterial);
            rightMat.SetFloat("_Seed", entry.stampSeed);
            rightImage.material = rightMat;

            rightButton.interactable = true;
            rightButton.onClick.RemoveAllListeners();
            rightButton.onClick.AddListener(() =>
            {
                ZooUIController.instance.ShowAnimalFromNotebook(entry);
                notebookPanel.SetActive(false);
            });
        }
        else
        {
            rightImage.gameObject.SetActive(false);
            rightLabel.gameObject.SetActive(false);
            rightButton.interactable = false;
        }

        // -------------------------
        // NAVIGATION BUTTONS
        // -------------------------
        arrowLeft.gameObject.SetActive(currentPage > 0);
        arrowRight.gameObject.SetActive(currentPage < maxPage);
    }

    public void OnLeftAnimalClicked()
    {
        int index = currentPage * 2;
        if (index < ZooNotebookManager.Instance.collectedAnimals.Count)
        {
            ZooUIController.instance.ShowAnimalFromNotebook(ZooNotebookManager.Instance.collectedAnimals[index]);
        }
    }
    public void OnRightAnimalClicked()
    {
        int index = currentPage * 2 + 1;
        if (index < ZooNotebookManager.Instance.collectedAnimals.Count)
        {
            ZooUIController.instance.ShowAnimalFromNotebook(ZooNotebookManager.Instance.collectedAnimals[index]);
        }
    }

    public void NextPage()
    {
        currentPage++;
        RefreshNotebook();
        ZooMusicManager.Instance.PlaySFX(4);
    }

    public void PrevPage()
    {
        currentPage--;
        RefreshNotebook();
        ZooMusicManager.Instance.PlaySFX(4);
    }

    private void ClearPage()
    {
        leftImage.gameObject.SetActive(false);
        rightImage.gameObject.SetActive(false);
        leftLabel.text = "";
        rightLabel.text = "";
    }
}
